package com.intellagric.service.impl.module.sys_area;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysAreaMapper;
import com.intellagric.pojo.SysArea;
import com.intellagric.pojo.SysAreaExample;
import com.intellagric.pojo.SysMenu;
import com.intellagric.service.module.sys_area.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/30
 * @DESC： 地区管理服务接口实现
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private SysAreaMapper areaMapper;

    /**
     * 添加区域位置信息
     * @param sysArea
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addArea(SysArea sysArea) {
        if (areaMapper.insertSelective(sysArea) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除区域位置信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteArea(String id) {
        if (areaMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改区域位置信息
     * @param sysArea
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editArea(SysArea sysArea) {
        if (areaMapper.updateByPrimaryKeySelective(sysArea) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.success();
        }
    }

    /**
     * 通过id查询区域位置信息
     * @param id
     * @return SysArea
     */
    @Override
    public SysArea getAreaById(String id) {
        return areaMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询区域位置信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getAreaList() {
        SysAreaExample areaExample = new SysAreaExample();
        areaExample.createCriteria().andIdIsNotNull();
        List<SysArea> areaList = areaMapper.selectByExample(areaExample);
        return LayuiDataGridResult.success().add(areaList,areaList.size());
    }

    /**
     * 分页查询区域位置信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getAreaPage(int page ,int rows) {
        PageHelper.startPage(page,rows);
        SysAreaExample areaExample = new SysAreaExample();
        areaExample.createCriteria().andIdIsNotNull();
        List<SysArea> areaList = areaMapper.selectByExample(areaExample);
        return LayuiDataGridResult.success().add(areaList,areaList.size());
    }

    /**
     * 查询区域位置信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getAreaTree() {
        List<SysArea> areaList = this.getAreaList().getData();

        SysAreaExample areaExample = new SysAreaExample();
        areaExample.createCriteria().andParentIdEqualTo("0");
        List<SysArea> rootAreaList = areaMapper.selectByExample(areaExample);

        for (SysArea sysArea : rootAreaList) {
            sysArea.setChildren(getChild(sysArea.getId(), areaList));
        }
        return LayuiDataGridResult.success().add(rootAreaList,rootAreaList.size());
    }

    private List<SysArea> getChild(String id, List<SysArea> areaList) {
        // 子菜单
        List<SysArea> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (SysArea sysArea : areaList) {
            if (sysArea.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (sysArea.getParentId().equals(id)) {
                    childList.add(sysArea);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (SysArea sysArea : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            sysArea.setChildren(getChild(sysArea.getId(), areaList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
